package com.yysoft.core.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	static public final String FORMAT_DATE_ONLY = "yyyy-MM-dd";

    static public final String FORMAT_TIME_ONLY = "HH:mm:ss";

    static public final String FORMAT_COMPACT = "yyyyMMddHHmmss";

    static public final String FORMAT_COMPACT_DATE = "yyyyMMdd";

    static public final String FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss";

    static public final String FORMAT_DETAIL = "yyyy-MM-dd HH:mm:ss.SSS";

    static public final String FORMAT_NO_SECOND = "yyyy-MM-dd HH:mm";

    static public final String FORMAT_NORMAL1 = "yyyy/MM/dd HH:mm";
    
	public static Timestamp parseTimestamp(String dateStr) {
		return parseTimestamp(dateStr, "yyyy-MM-dd HH:mm:ss");
	}

	public static Timestamp parseTimestamp(String dateStr, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			Date date = sdf.parse(dateStr);
			return new Timestamp(date.getTime());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String parseString(Date date) {
		return parseString(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String parseString(Date date, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		return sdf.format(date);
	}
	
	public static String getDate(String format) {
		return parseString(new Date(), format);
	}
	
	public static Timestamp getNowTimestamp(){
		return new Timestamp(new Date().getTime());
	}
	
	public static Date addDate(Date date, int years, int months, int dates, int hours, int minutes, int seconds) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, years);
		calendar.add(Calendar.MONTH, months);
		calendar.add(Calendar.DATE, dates);
		calendar.add(Calendar.HOUR, hours);
		calendar.add(Calendar.MINUTE, minutes);
		calendar.add(Calendar.SECOND, seconds);
		return calendar.getTime();
	}
	
	public static String getYesterday(String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return parseString(calendar.getTime(), format);
	}
	
}
